package com.googlecode.cswish.struts.conversion;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.googlecode.cswish.model.ModelInfo;
import com.googlecode.cswish.model.PropertyInfo;
import com.googlecode.cswish.struts.spring.BeanIntrospector;
import com.googlecode.cswish.struts.spring.ObjectFactory;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.reflection.ReflectionProvider;

/**
 * Call BeanIntrospector to get the correct List type
 * 
 * @author feng Date: 2009-4-3
 */
public class DefaultObjectTypeDeterminer extends
		com.opensymphony.xwork2.conversion.impl.DefaultObjectTypeDeterminer {
	protected final Log logger = LogFactory.getLog(getClass());
	
	private BeanIntrospector beanIntrospector;

	@Inject
	public void setObjectFactory(
			com.opensymphony.xwork2.ObjectFactory objectFactory) {
		ObjectFactory factory = (ObjectFactory) objectFactory;
		beanIntrospector = factory.getBean(BeanIntrospector.class, false);
	}

	@Inject
	public DefaultObjectTypeDeterminer(@Inject XWorkConverter conv,
			@Inject ReflectionProvider prov) {
		super(conv, prov);
	}
	
	@Override
	public Class getElementClass(Class parentClass, String property, Object key) {
		ModelInfo modelInfo = beanIntrospector.getModelInfo(parentClass.getName());
		Class clazz = null;
		if (modelInfo != null) {
			PropertyInfo propertyInfo = modelInfo.getProperty(property);
			clazz = propertyInfo.getGenericTypeClass();
		}
		return clazz != null ? clazz : super.getElementClass(parentClass, property, key);
	}
}
